//
//  WDArrivalServiceHeaderView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/4.
//

#import "WDArrivalServiceHeaderView.h"
#import "ZHCustomUserInfoModel.h"

@interface WDArrivalServiceHeaderView ()

@property (nonatomic, strong) ZHCustomUserInfoModel *userInfoModel;

@property (nonatomic, strong) UIImageView *headerImgView;

/// 用户姓名
@property (nonatomic, strong) UILabel *nameLabel;

/// 岗位
@property (nonatomic, strong) UILabel *postNameLabel;

/// 电话
@property (nonatomic, strong) UILabel *phoneLabel;

/// 服务站名称
@property (nonatomic, strong) UILabel *companyNameLabel;;


/// 用户姓名
- (void)initWithNameLabel;

/// 岗位
- (void)initWithPostNameLabel;

/// 电话
- (void)initWithPhoneLabel;

/// 服务站名称
- (void)initWithCompanyNameLabel;

@end

@implementation WDArrivalServiceHeaderView

- (void)initWithVar
{
    [super initWithVar];
//    [EDSAppDelegateTool sharedInstance].userInfoModel;
}

- (void)initWithSubView
{
    [super initWithSubView];
    
    self.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    
    [self initWithHeaderImgView];
    
    /// 用户姓名
    [self initWithNameLabel];

    /// 岗位
    [self initWithPostNameLabel];

    /// 电话
    [self initWithPhoneLabel];

    /// 服务站名称
    [self initWithCompanyNameLabel];

}

- (void)initWithHeaderImgView
{
    if (!_headerImgView) {
        CGRect frame = CGRectMake(20, 0, 80, 80);
        _headerImgView = [[UIImageView alloc] initWithFrame:frame];
//        _headerImgView.backgroundColor = [UIColor orangeColor];
        _headerImgView.layer.cornerRadius = frame.size.height * 0.5;
        _headerImgView.layer.masksToBounds = YES;
        [self addSubview:_headerImgView];
        _headerImgView.userInteractionEnabled = YES;
        @weakify(self);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self);
            [self actionWithHeaderImg:nil];
        }];
        [_headerImgView addGestureRecognizer:tap];
    }
    if (self.userInfoModel) {
        [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:self.userInfoModel.avatar] placeholderImage:[UIImage imageNamed:@"ic_default_head_portrait"]];
    }
}

/// 用户姓名
- (void)initWithNameLabel {
    if (!_nameLabel) {
        NSString *str = @"张三";
        UIFont *font = [UIFont systemFontOfSize:20 weight:UIFontWeightSemibold];
        float width = [str widthForFont:font];
        CGRect frame = CGRectMake(self.headerImgView.right + 16, self.headerImgView.top, width, 28);
        _nameLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
//            label.text = str;
        }];
    }
    if (self.userInfoModel) {
        NSString *str = objectNull(self.userInfoModel.realName);
        float width = [str widthForFont:_nameLabel.font] + 2;
        _nameLabel.width = width;
        _nameLabel.text = str;
    }
}

/// 岗位
- (void)initWithPostNameLabel {
    if (!_postNameLabel) {
        NSString *str = @"维修员";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        float width = [str widthForFont:font];
        CGRect frame = CGRectMake(self.nameLabel.right + 10, self.headerImgView.top, width, 18);
        _postNameLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            // label.text = str;
            label.backgroundColor = [UIColor colorWithHexString:@"#19C46E"];
            label.textColor = [UIColor whiteColor];
            label.layer.cornerRadius = 2;
            label.layer.masksToBounds = YES;
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    if (self.userInfoModel) {
        NSString *postName = objectNull(self.userInfoModel.roleName);
        postName = [postName stringByReplacingOccurrencesOfString:@"服务站-" withString:@""]; // 去掉“服务站”几个字
        postName = [postName stringByReplacingOccurrencesOfString:@"服务站" withString:@""];
        float width = [postName widthForFont:_postNameLabel.font] + 10;
        float maxWidth = kScreenW - self.nameLabel.right - 10 - 16;
        width = (width > maxWidth ? maxWidth : width);
        _postNameLabel.text = postName;
        _postNameLabel.width = width;
        _postNameLabel.centerY = self.nameLabel.centerY;
        _postNameLabel.left = self.nameLabel.right + 10;
    }
}

/// 电话
- (void)initWithPhoneLabel {
    if (!_phoneLabel) {
        NSString *str = @"18261929604";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        float width = [str widthForFont:font];
        CGRect frame = CGRectMake(self.nameLabel.left, self.nameLabel.bottom + 16, width, 18);
        _phoneLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
//            label.text = str;
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    if (self.userInfoModel) {
        NSString *str = objectNull(self.userInfoModel.phone);
        float width = [str widthForFont:_phoneLabel.font] + 6;
        _phoneLabel.width = width;
        _phoneLabel.centerY = self.headerImgView.centerY;
        _phoneLabel.text = str;
    }
}

/// 服务站名称
- (void)initWithCompanyNameLabel {
    if (!_companyNameLabel) {
//        NSString *str = @"二维翁温热无若我认为服务站";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        float width = self.width - self.nameLabel.left - 18;//[str widthForFont:font];
        CGRect frame = CGRectMake(self.nameLabel.left, self.headerImgView.bottom, width, 16);
        _companyNameLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
//        _companyNameLabel.bottom = self.headerImgView.bottom;
    }
    if (self.userInfoModel) {
        self.companyNameLabel.text = objectNull(self.userInfoModel.stationName);
        self.companyNameLabel.top = self.phoneLabel.bottom + 4;
    }
}

/// 点击头像
/// - Parameter sender: sender description
- (void)actionWithHeaderImg:(id)sender
{
    if (self.touchHeaderImageBlock) {
        self.touchHeaderImageBlock();
    }
}

- (void)updateWithViewData:(id)data
{
    self.userInfoModel = [EDSAppDelegateTool sharedInstance].userInfoModel;
    
    [self initWithHeaderImgView];
    
    /// 用户姓名
    [self initWithNameLabel];

    /// 岗位
    [self initWithPostNameLabel];

    /// 电话
    [self initWithPhoneLabel];

    /// 服务站名称
    [self initWithCompanyNameLabel];
}

@end
